package com.cloudsigns.factorymanager2.produce.bean;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.produce.utils.FormatTime;
import com.cloudsigns.factorymanager2.produce.utils.ParseJson;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class WorkPartBean implements Serializable {
    private long adjustCompleteDate;//	变更之后的完成时间
    private int amount;// 工序需要加工的数量
    private long completeDate;// 完成时间
    private String engineeringName;// 工程名称
    private boolean lastWorkPart;// 是否是最后一道工序标记
    private CommonEnum.ProduceOrderLevel level;// 订单级别：非常重要、比较重要、一般重要、正常 必须为以下可用值之一: [ "VERY_IMPORTANT", "MORE_IMPORTANT", "GENERALLY_IMPORTANT", "NORMAL" ]
    private String orderId;// 冗余 生产订单Id
    private String productId;//	产品Id
    private String productName;// 产品名称
    private String projectName;// 项目名称
    private boolean shapeMap;//	具体规格见图纸

    private ShapeTypeBean shapeType = new ShapeTypeBean();

    private long startDate;// 开始时间
    private String type;// 订单类别
    private String unit;// 单位
    private String workPartId;// 工序Id
    private String workPartName;// 工序名称
    public boolean isChecked;// UI里用到 非对象成员变量
    private CommonEnum.ProduceOrderStatus status;

    private ArrayList<WorkPartOrderBean> workPartOrderList = new ArrayList<>();
    public ArrayList<WorkPartScheduleBean> workPartScheduleList = new ArrayList<>();

    // 判断是否已编辑 增加一条加工信息
    public boolean isEdit() {
        if (amount > 0 && startDate > 0 && completeDate > 0) {
            return true;
        }
        return false;
    }

    public long getAdjustCompleteDate() {
        return adjustCompleteDate;
    }

    public void setAdjustCompleteDate(long adjustCompleteDate) {
        this.adjustCompleteDate = adjustCompleteDate;
    }

    public int getAmount() {
        return amount;
    }

    public String getAmountStr() {
        if (amount == 0) {
            return "";
        }
        return String.valueOf(amount);
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public long getCompleteDate() {
        return completeDate;
    }

    public void setCompleteDate(long completeDate) {
        completeDate = completeDate + 24 * 3600 * 1000 - 2;
        this.completeDate = completeDate;
    }

    public String getEngineeringName() {
        return engineeringName;
    }

    public void setEngineeringName(String engineeringName) {
        this.engineeringName = engineeringName;
    }

    public boolean isLastWorkPart() {
        return lastWorkPart;
    }

    public void setLastWorkPart(boolean lastWorkPart) {
        this.lastWorkPart = lastWorkPart;
    }

    public CommonEnum.ProduceOrderLevel getLevel() {
        return level;
    }

    public void setLevel(CommonEnum.ProduceOrderLevel level) {
        this.level = level;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public boolean getShapeMap() {
        return shapeMap;
    }

    public void setShapeMap(boolean shapeMap) {
        this.shapeMap = shapeMap;
    }

    public boolean isShapeMap() {
        return shapeMap;
    }

    public ShapeTypeBean getShapeType() {
        return shapeType;
    }

    public void setShapeType(ShapeTypeBean shapeType) {
        this.shapeType = shapeType;
    }

    public long getStartDate() {
        return startDate;
    }

    public void setStartDate(long startDate) {
        this.startDate = startDate;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getWorkPartId() {
        return workPartId;
    }

    public void setWorkPartId(String workPartId) {
        this.workPartId = workPartId;
    }

    public String getWorkPartName() {
        return workPartName;
    }

    public void setWorkPartName(String workPartName) {
        this.workPartName = workPartName;
    }

    public ArrayList<WorkPartOrderBean> getWorkPartOrderList() {
        return workPartOrderList;
    }

    public void setWorkPartOrderList(ArrayList<WorkPartOrderBean> workPartOrderList) {
        this.workPartOrderList = workPartOrderList;
    }

    public ArrayList<WorkPartScheduleBean> getWorkPartScheduleList() {
        return workPartScheduleList;
    }

    // listview item计划数量总和
    public int getScheduleAmountAll() {
        int amount = 0;
        for (WorkPartScheduleBean workPartScheduleBean : workPartScheduleList) {
            amount += workPartScheduleBean.getAmount();
        }
        return amount;
    }

    public void setWorkPartScheduleList(ArrayList<WorkPartScheduleBean> workPartScheduleList) {
        this.workPartScheduleList = workPartScheduleList;
    }

    public CommonEnum.ProduceOrderStatus getStatus() {
        return status;
    }

    public void setStatus(CommonEnum.ProduceOrderStatus status) {
        this.status = status;
    }

    public static long getOneDayMs() {
        return ONE_DAY_MS;
    }

    public static void setOneDayMs(long oneDayMs) {
        ONE_DAY_MS = oneDayMs;
    }

    /**
     * 删除工序加工信息
     */
    public void resetDefault() {
        amount = 0;
        startDate = 0;
        completeDate = 0;
        workPartScheduleList.clear();
        shapeMap = false;
        //  isEdit=false;
        unit = "";
        shapeType = new ShapeTypeBean();
    }

    private static long ONE_DAY_MS = 24 * 60 * 60 * 1000;

    /**
     * 计算两个日期之间的日期
     */
    public void betweenDays() {
        if (startDate < completeDate && startDate > 0) {
            workPartScheduleList.clear();

            Date date_start = new Date(startDate);
            Date date_end = new Date(completeDate);

            //计算日期从开始时间于结束时间的0时计算
            Calendar fromCalendar = Calendar.getInstance();
            fromCalendar.setTime(date_start);
            fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
            fromCalendar.set(Calendar.MINUTE, 0);
            fromCalendar.set(Calendar.SECOND, 0);
            fromCalendar.set(Calendar.MILLISECOND, 0);

            Calendar toCalendar = Calendar.getInstance();
            toCalendar.setTime(date_end);
            toCalendar.set(Calendar.HOUR_OF_DAY, 0);
            toCalendar.set(Calendar.MINUTE, 0);
            toCalendar.set(Calendar.SECOND, 0);
            toCalendar.set(Calendar.MILLISECOND, 0);

            int s = (int) ((toCalendar.getTimeInMillis() - fromCalendar.getTimeInMillis()) / (ONE_DAY_MS));
            WorkPartScheduleBean wp = null;

            if (s > 0) {
                long todayDate = 0;
                for (int i = 0; i <= s; i++) {
                    todayDate = fromCalendar.getTimeInMillis() + i * ONE_DAY_MS;
                    wp = new WorkPartScheduleBean();
//                    wp.setStrTime(FormatTime.getCustonFormatTime(todayDate, "yyyy-MM-dd"));
                    wp.setScheduleDate(todayDate);
                    workPartScheduleList.add(wp);
                }
            } else {//此时在同一天之内
                wp = new WorkPartScheduleBean();
//                wp.setStrTime(FormatTime.getCustonFormatTime(startDate, "yyyy-MM-dd"));
                wp.setScheduleDate(startDate);
                workPartScheduleList.add(wp);
            }
        }
    }


    public void parseJson(JSONObject workPartListJsonObj) throws JSONException {
        if (workPartListJsonObj.has("adjustCompleteDate")) {
            String adjustCompleteDateStr = workPartListJsonObj.getString("adjustCompleteDate");
            if (!TextUtils.isEmpty(adjustCompleteDateStr) && !adjustCompleteDateStr.equals("null")) {
                setAdjustCompleteDate(Long.parseLong(adjustCompleteDateStr));
            }
        }
        if (workPartListJsonObj.has("amount")) {
            String amountStrw = workPartListJsonObj.getString("amount");
            if (null != amountStrw && !TextUtils.isEmpty(amountStrw) && !"null".equals(amountStrw)) {
                setAmount(Integer.parseInt(amountStrw));
            }
        }
        if (workPartListJsonObj.has("completeDate")) {
            String completeDateStr = workPartListJsonObj.getString("completeDate");
            if (!TextUtils.isEmpty(completeDateStr) && !completeDateStr.equals("null")) {
                setCompleteDate(Long.parseLong(completeDateStr));
            }
        }
        if (workPartListJsonObj.has("engineeringName")) {
            setEngineeringName(workPartListJsonObj.getString("engineeringName"));
        }
        if (workPartListJsonObj.has("lastWorkPart")) {
            setLastWorkPart(workPartListJsonObj.getBoolean("lastWorkPart"));
        }
        if (workPartListJsonObj.has("level")) {
            CommonEnum.ProduceOrderLevel level = ParseJson.parseProdutOrderLevel(workPartListJsonObj, "level");
            setLevel(level);
        }
        if (workPartListJsonObj.has("orderId")) {
            setOrderId(workPartListJsonObj.getString("orderId"));
        }
        if (workPartListJsonObj.has("productId")) {
            setProductId(workPartListJsonObj.getString("productId"));
        }
        if (workPartListJsonObj.has("productName")) {
            setProductName(workPartListJsonObj.getString("productName"));
        }
        if (workPartListJsonObj.has("projectName")) {
            setProjectName(workPartListJsonObj.getString("projectName"));
        }
        if (workPartListJsonObj.has("shapeMap")) {
            setShapeMap(workPartListJsonObj.getBoolean("shapeMap"));
        }
        if (workPartListJsonObj.has("shapeType")) {
            JSONObject shapeTypeJsonObjw = workPartListJsonObj.getJSONObject("shapeType");
            if (null != shapeTypeJsonObjw) {
                getShapeType().parseObject(shapeTypeJsonObjw);
            }
        }
        if (workPartListJsonObj.has("startDate")) {
            String startDateStr = workPartListJsonObj.getString("startDate");
            if (!TextUtils.isEmpty(startDateStr) && !startDateStr.equals("null")) {
                setStartDate(Long.parseLong(startDateStr));
            }
        }
        if (workPartListJsonObj.has("type")) {
            setType(workPartListJsonObj.getString("type"));
        }
        if (workPartListJsonObj.has("unit")) {
            setUnit(workPartListJsonObj.getString("unit"));
        }
        if (workPartListJsonObj.has("workPartId")) {
            setWorkPartId(workPartListJsonObj.getString("workPartId"));
        }
        if (workPartListJsonObj.has("workPartName")) {
            setWorkPartName(workPartListJsonObj.getString("workPartName"));
        }
        if (workPartListJsonObj.has("workPartOrderList")) {
            String workPartOrderListJsonArrStr = workPartListJsonObj.getString("workPartOrderList");
            workPartOrderList.clear();
            if (null != workPartOrderListJsonArrStr && !workPartOrderListJsonArrStr.equals("null") && !TextUtils.isEmpty(workPartOrderListJsonArrStr)) {
                JSONArray workPartOrderListJsonArr = new JSONArray(workPartOrderListJsonArrStr);
                for (int y = 0; y < workPartOrderListJsonArr.length(); y++) {
                    WorkPartOrderBean workPartOrderBean = new WorkPartOrderBean();
                    workPartOrderBean.setParentId(orderId, productId, workPartId);
                    JSONObject workPartOrderListJsonObj = (JSONObject) workPartOrderListJsonArr.get(y);
                    workPartOrderBean.parseJson(workPartOrderListJsonObj);

                    this.workPartOrderList.add(workPartOrderBean);
                }
            }
        }
        if (workPartListJsonObj.has("workPartScheduleList")) {
            String workPartScheduleListJsonArrStr = workPartListJsonObj.getString("workPartScheduleList");
            workPartScheduleList.clear();
            if (null != workPartScheduleListJsonArrStr && !workPartScheduleListJsonArrStr.equals("null") && !TextUtils.isEmpty(workPartScheduleListJsonArrStr)) {
                JSONArray workPartScheduleListJsonArr = new JSONArray(workPartScheduleListJsonArrStr);
                for (int j = 0; j < workPartScheduleListJsonArr.length(); j++) {
                    WorkPartScheduleBean workPartScheduleBean = new WorkPartScheduleBean();
                    JSONObject workPartScheduleListJsonObj = (JSONObject) workPartScheduleListJsonArr.get(j);
                    workPartScheduleBean.parseJson(workPartScheduleListJsonObj);
                    workPartScheduleList.add(workPartScheduleBean);
                }
            }
        }
    }

    /**
     * 获取该工序已经编辑的工序工单的生产总数
     *
     * @return 剩余数量
     */
    public int getWorkpartOrderProduceCount() {
        int count = 0;
        for (WorkPartOrderBean workPartOrderBean : workPartOrderList) {
            count += workPartOrderBean.getAmount();
        }
        return count;
    }

    /**
     * 删除工序派工信息
     *
     * @param workPartOrderBean
     */
    public void removeWorkpartOrderByIdOrOtherMessage(WorkPartOrderBean workPartOrderBean) {
        if (TextUtils.isEmpty(workPartOrderBean.getWorkPartOrderId())) {
            for (WorkPartOrderBean wpOrderBean : workPartOrderList) {
                if (workPartOrderBean.getAmount() == wpOrderBean.getAmount() &&
                        workPartOrderBean.getAssignReceiveAccountId().equals(wpOrderBean.getAssignReceiveAccountId())) {
                    workPartOrderList.remove(wpOrderBean);
                    return;
                }
            }
        } else {
            for (WorkPartOrderBean wpOrderBean : workPartOrderList) {
                if (workPartOrderBean.getWorkPartOrderId().equals(wpOrderBean.getWorkPartOrderId())) {
                    workPartOrderList.remove(wpOrderBean);
                    return;
                }
            }
        }
        return;
    }

    public WorkPartOrderBean getWorkPartOrderById(String workPartOrderId) {
        if (TextUtils.isEmpty(workPartOrderId)) {
            return null;
        }
        for (WorkPartOrderBean wpOrderBean : workPartOrderList) {
            if (workPartOrderId.equals(wpOrderBean.getWorkPartOrderId())) {
                return wpOrderBean;
            }
        }
        return null;
    }

    /**
     * 某一天的计划数量
     *
     * @param time
     * @return
     */
    public WorkPartScheduleBean getScheduleBeanByTime(long time) {
        String strT = DateUtil.getYyyy_Mm_Dd(time);
        for (WorkPartScheduleBean sbean : workPartScheduleList) {
            if (DateUtil.getYyyy_Mm_Dd(sbean.getScheduleDate()).equals(strT)) {
                return sbean;
            }
        }
        return null;
    }

}
